<script lang="ts" setup>
import { ref } from "vue";
import type { Ref } from "vue";
import { addVisitor } from "@/api/visitor";
import { useRouter } from "vue-router";
import { showNotify } from "vant";

const router = useRouter();
const tel: Ref<string> = ref("");
const age: Ref<string> = ref("");
const gender: Ref<string> = ref("");
const name: Ref<string> = ref("");
const code: Ref<string> = ref("");
const number: Ref<string> = ref("");
// 获取input框的内容
const inputContent: Ref<string> = ref("身份证");
const isActive: Ref<number> = ref(0);
function getText(index: number, text: string) {
  isActive.value = index;
  // e.target.classList.add("active");

  inputContent.value = text;
}
// 点击确认添加
function addVisitorList() {
  if (!tel.value || !name.value || !code.value || !age.value || !gender.value) {
    return showNotify({ type: "warning", message: "内容不能为空" });
  }
  if (!/^(?:[\u4e00-\u9fa5·]{2,16})$/.test(name.value)) {
    return showNotify({ type: "warning", message: "姓名请输入中文" });
  }
  if (!/^(?:(?:\+|00)86)?1[3-9]\d{9}$/.test(tel.value)) {
    return showNotify({ type: "warning", message: "请输入正确手机号" });
  }
  if (!/^(男|女|保密)$/.test(gender.value)) {
    return showNotify({ type: "warning", message: "请输入男|女|保密" });
  }
  if (
    !/^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\d|30|31)\d{3}[\dXx]$/.test(
      number.value
    )
  ) {
    return showNotify({ type: "warning", message: "请输入正确身份证号" });
  }
  addVisitor({
    username: tel.value,
    name: name.value,
    cardid: number.value,
    age: age.value,
    gender: gender.value,
  }).then((res) => {
    showNotify({ type: "success", message: "添加成功" });
    router.back();
  });
}
const arr = [
  { text: "身份证", index: 0 },
  { text: "护照", index: 1 },
  { text: "军官证", index: 2 },
  { text: "港澳通行证", index: 3 },
];
</script>

<template>
  <div>
    <van-nav-bar left-arrow @click-left="$router.back()" />
    <div class="main">
      <h1>添加就诊人</h1>
      <van-field v-model="name" label="真实姓名" />
      <van-field v-model="age" label="年龄" />
      <van-field v-model="gender" label="性别" />
      <p>证件类型</p>
      <div class="input_box">
        <van-button
          v-for="item in arr"
          :class="isActive === item.index ? 'active' : ''"
          :key="item.index"
          @click="getText(item.index, item.text)"
          :value="item.text"
          >{{ item.text }}</van-button
        >
        <!-- <van-button @click="getText" value="护照">护照</van-button>
        <van-button @click="getText" value="军官证">军官证</van-button>
        <van-button @click="getText" value="港澳通行证">港澳通行证</van-button> -->
      </div>
      <van-field v-model="number" type="number" :label="inputContent" />
      <van-field v-model="tel" type="tel" label="手机号" />
      <van-field v-model="code" type="digit" label="验证码" />
      <van-button class="addButton" type="primary" @click="addVisitorList">
        添加就诊人
      </van-button>
    </div>
  </div>
</template>
<style lang="scss" scoped>
.van-nav-bar {
  background-color: #f7fdff;
}
.main {
  padding: 0.1rem 0.3rem;
  display: flex;
  flex-direction: column;
  h1 {
    font-size: 0.3rem;
    margin-bottom: 0.2rem;
  }
  .input_box {
    display: flex;
    justify-content: space-between;
    margin-bottom: 0.05rem;
    .van-button {
      font-size: 0.16rem;
      height: 0.3rem;
      background-color: #fff;
      border: none;
      margin: 0;
      padding: 0.05rem;
      border-radius: 0.05rem;
    }
    .active {
      background-color: skyblue;
    }
  }
  .addButton {
    align-self: center;
    width: 50%;
    border-radius: 0.15rem;
    font-weight: 700;
    color: #fff;
    margin-top: 0.4rem;
  }
}
</style>
